<?php

$url = "https://mp.weixin.qq.com/cgi-bin/operate_appmsg?sub=weapp_qrcode";
$path = isset($_POST['path']) ? $_POST['path'] : '';

if (isset($path)) {

//    echo $path;
    $data = [
        "appid" => "wx992fcdbfa4651f0f",
        "path" => $path,
        "token" => "2102647284",
        "lang" => "zh_CN",
        "f" => "json",
        "ajax" => "1",
    ];
    $headers = "";

    $data = http_build_query($data);
    $result = post($url, $data, $headers = []);

    $data = json_decode($result, 1);

    if(isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == 'xmlhttprequest'){
        // 是ajax请求
        echo json_encode($data);
        die;
    }

    if($data["base_resp"]["ret"] === 0){
        $codeUrl = "/tools/huiju/huiju/getimage.php?data=".$data["qrcode_url"];
    }else{
        echo json_encode($data);
        die;
    }

//    $img = file_get_contents($codeUrl);
//    echo $img;
//    die;
}

function post($url, $data, $headers)
{

    //初使化init方法
    $ch = curl_init();
    //指定URL
    curl_setopt($ch, CURLOPT_URL, $url);
    //设定请求后返回结果
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    //声明使用POST方式来进行发送
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_REFERER, "https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit&action=edit&type=10&appmsgid=100000003&token=2102647284&lang=zh_CN");
    //发送什么数据呢
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $cookie = "appmsglist_action_2398002642=card; appmsglist_action_2392614199=card; appmsglist_action_3207748393=card; pgv_pvi=3755698176; pgv_pvid=2862409315; ua_id=KVwBhgVmyolDlimcAAAAAARycF6lhCb3qRHGfMwRlog=; mm_lang=zh_CN; RK=M7S0xvlSY9; ptcz=8bc20e3d4612e7e3a0c3b68f711606cb25e191eea7915679f7c1d11e29a5d06e; tvfe_boss_uuid=94a052953ad1e192; _ga=GA1.2.429760441.1592275069; iip=0; pt_sms_phone=134******51; eas_sid=n1t5y9J7c2V8r5n2m8x7u683V8; sd_userid=71601598865205020; sd_cookie_crttime=1598865205020; o_cookie=1489051248; pac_uid=1_1489051248; _tc_unionid=e0c0080a-c9c5-403e-bb6e-bb5055fb645c; ied_qq=o1489051248; openid2ticket_of8wFw3RtEzLgtMG7OGXc-jqZyAM=; openid2ticket_oYRmE5-5DuZaCxmdQNhK3xogQkd8=; mobileUV=1_178ce75002c_280dc; OUTFOX_SEARCH_USER_ID_NCOO=1268910667.0090034; wxuin=21846765420700; FTN5K=e7c6b2dc; rewardsn=; wxtokenkey=777; _qpsvr_localtk=1626139702132; xm_wwticket=CloKBAgBEAASIA7nrbHjVU2RDvdGUQI2oOJMs1hQ5Lr8dT5Stv5cJdOJGjBye3sd+HeOUKIVrccFzEDnsOeL5MgxEEDKWcC+e0NLYPyl0DgQAKrrk2rydm7IsB4iCAoGCAAQABgA; wedrive_uin=1688850522808011; wedrive_sid=AMtreQCeM3oGfTJSACdpNAAA; wedrive_sids=13102669735675039&z585eYxEdTUuNF9GAAdfLQAA|1688850522808011&AMtreQCeM3oGfTJSACdpNAAA; wedrive_skey=13102669735675039&7a2a9c6277f8ef61f1d1bcb71ad3a3f0|1688850522808011&871cc6cdb81fabd9f29db5115eea7fa2; wedrive_ticket=13102669735675039&CAESICkZnxfbdTY1I9bDGF5e0Xdkgn8PjDiE7sO88gUt2Mq1|1688850522808011&CAESIFsIcVn9kp8BbXV1wHxJn5dn8WrrbHUkcVczdSsYRUUT; authkey=70080010021860226050411080cc883db61032f1cf58ae37b2be8ccf543beab5659a0a7a899637fa72a2bdd35ad31da61c1617541455c042a29a907c6ab8d3aec35ca46bd42c16840145f139a7bc3a67ec819a7f3c505a23c7a8b995c0f812e7599f7a0723613f354a&weixinnum=1688850522808011; uuid=ed72a05a2dead26622e57b1295be56d0; rand_info=CAESIAAuHVlz463OExFDWAmjzb/KrELQX+tYE+Ct5f3CiyO0; slave_bizuin=3207748393; data_bizuin=3207748392; bizuin=3207748393; data_ticket=UJTiHD62PFvbkchT3U1edEGXsJH3forDkYGfMtDWkApgIWr2gdBqGb462VsXnzOY; slave_sid=RjV3V3I0b0YyWDBjVk9Zc0ppTWRFMjd6TEtMT1NXVmZXQ2RQT2dWVTJ5dERWcVdYRXBLVjlYZWdSWVpvM3J4RHpFd0JvSEdfTExuY3JTV3IyT2FFTFdONTk4Tk40WFpKRUJXWk9QSWRYeHhWczRmQUtMTlVwRk54MDVDbFVQMmZLM085TG0yaG1QcUM1WkxD; slave_user=gh_e52093de47d9; xid=49673fe6d3fd626fa36bc25dfcb1968d";
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36');

    //忽略证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

    //忽略header头信息
    curl_setopt($ch, CURLOPT_HEADER, 0);

    //设置超时时间
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

    //发送请求
    $output = curl_exec($ch);

    //关闭curl
    curl_close($ch);

    //返回数据
    return $output;
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php if(isset($codeUrl)): ?>
<a href="javascript:window.open('<?php echo $codeUrl; ?>')">新窗口扫码</a><?php echo $codeUrl; ?>
<img src="<?php echo $codeUrl; ?>" />
<?php endif; ?>
<form action="" method="post">
    <input type="text" name="path" value="">
    <input type="submit" value="提交路径">
</form>
</body>
</html>